// Virtual PoleDoc.h : interface of the CVirtualPoleDoc class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_VIRTUALPOLEDOC_H__CC589CC7_F6BF_4E33_9E5A_AD4F727122EC__INCLUDED_)
#define AFX_VIRTUALPOLEDOC_H__CC589CC7_F6BF_4E33_9E5A_AD4F727122EC__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "MainFrm.h"
#include "niimaq.h"
#include "fftw3.h"
#include "NIDAQmx.h"

// prototype (function typedef) for DLL function Inp32
short _stdcall Inp32(short PortAddress);
void _stdcall Out32(short PortAddress, short data);

// defines the element types we are going to use in the array to be medianed
typedef unsigned short elem_type;

class CVirtualPoleDoc : public CDocument {
protected: // create from serialization only
	CVirtualPoleDoc();
	DECLARE_DYNCREATE(CVirtualPoleDoc)

// Attributes
public:
	double m_LastDAQOutput[2];

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CVirtualPoleDoc)
	public:
	virtual BOOL OnNewDocument();
	//}}AFX_VIRTUAL

// Implementation
public:
	void TriggerSave();
	Result_Params m_Results;			// results returned for displaying
	
	// image buffers
	byte *m_pAnalyzeBuffer, *m_pRawBuffer, *m_pDarkFrameBuffer, *m_pMotionMaskBuffer;
	byte *m_pCurrAnalyzedBuffer;
	
	//synchronization objects
	volatile bool m_bStopRing;
	volatile bool m_bStartSave;

	// functions
	CSize GetFrameSize();
	void CloseVideo();
	void TriggerStop();
	bool isDataLoded();
	void SignalContactOnParPort();
	void EnableExSyncPreview(bool bEnable, bool bUseTrig, bool bEnableTrigOut);
	void ExSyncReset();
	bool OnFileStartCaptureVideo(CRect *ROI=NULL, LPVOID pParam=NULL);
	bool OnStartAnalyze(LPVOID pParam);
	virtual ~CVirtualPoleDoc();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

// Generated message map functions
protected:
	//{{AFX_MSG(CVirtualPoleDoc)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
	
public:
	volatile bool	m_bResetSave;						 // a flag to restart the save thread
	unsigned long	m_iSavedFrameCounter;				 // the number of framea already saved to disk
	unsigned long	m_iMissedNbBuf;
	SESSION_ID		m_Sid;
	volatile ULONG	m_lLoop_Index;						 // the index where we currently write new frames to memory
	volatile ULONG	m_lWrite_Index;						 // the index where we currently write new frames to disk
	bool m_bMissedFramesReported;
	bool CreateDirectoryFromPath(CString csFilename);
	PULSE_ID m_plsExSyncTrain;
	PULSE_ID m_plsExSyncHalfCopy;
	PULSE_ID m_plsExSyncTrigOut;
	double m_dIntersect;
	double m_dSlope;

private:
	// Imaq variables
	INTERFACE_ID	m_Iid;
	CWinThread		* m_hStopThread; 
	CWinThread		* m_hAnalyzeThread;
	CWinThread		* m_hSaveThread;
	HANDLE			m_hStopEvent;
	HANDLE			m_hStartSave;
	HANDLE			m_hAnalysisStarted;
	int				m_iError;

	// analyzing params
	bool			m_bDataLoaded;
	CSize			m_FrameSize;
	byte			*m_pRingBuffer[RingBufferSize];
	unsigned short	m_MotionX[655360],m_MotionY[655360]; // maximum size is 1280 X 512
	CStdioFile		m_LogFile;							 // the file to write a log to
	dPoint			m_dpFactors;						 // factors to isolate only the movment which is parallel to the Medial Line
	//volatile bool	m_bResetSave;						 // a flag to restart the save thread	
	CTime			m_tNow;								 // the current time
	CString			m_csMissedFramesList;				 // list of missed frames numbers
	//unsigned long	m_iSavedFrameCounter;				 // the number of frames already saved to disk
	CString m_csCameraTriggerLine;

	// functions
	void DisplayImaqError(int error);
	bool AnalyzeProc();
	void StopProc();
	void SaveProc();
	static UINT StopThread(LPVOID pParam);
	static UINT AnalyzeThread(LPVOID pParam);
	static UINT SaveThread(LPVOID pParam);
	elem_type kth_smallest(elem_type a[], int n, int k);
public:
	bool m_bDarkFrameExists;
	int m_nDarkFrameMaxVal;
	Analyze_Params	*m_pParam_Struct;

};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line

#endif // !defined(AFX_VIRTUALPOLEDOC_H__CC589CC7_F6BF_4E33_9E5A_AD4F727122EC__INCLUDED_)
